﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using QLKHCN.Models.Entities;

namespace QLKHCN.Areas.Admin.Controllers
{
    public class NhanViensController : Controller
    {
        private KHCNDbContext db = new KHCNDbContext();

        // GET: Admin/NhanViens
        public async Task<ActionResult> Index()
        {
            var nhanViens = db.NhanViens.Include(n => n.CapBac).Include(n => n.ChucDanh).Include(n => n.ChucVu).Include(n => n.HocVi).Include(n => n.PhongBan);
            return View(await nhanViens.ToListAsync());
        }

        // GET: Admin/NhanViens/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            NhanVien nhanVien = await db.NhanViens.FindAsync(id);
            if (nhanVien == null)
            {
                return HttpNotFound();
            }
            return View(nhanVien);
        }

        // GET: Admin/NhanViens/Create
        public ActionResult Create()
        {
            ViewBag.CapBacID = new SelectList(db.CapBacs, "ID", "TenCapBac");
            ViewBag.ChucDanhID = new SelectList(db.ChucDanhs, "ID", "TenChucDanh");
            ViewBag.ChucVuID = new SelectList(db.ChucVus, "ID", "TenChucVu");
            ViewBag.HocViID = new SelectList(db.HocVis, "ID", "TenHocVi");
            ViewBag.PhongBanID = new SelectList(db.PhongBans, "ID", "TenPhongBan");
            return View();
        }

        // POST: Admin/NhanViens/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "ID,HoTen,ChucVuID,CapBacID,HocViID,ChucDanhID,PhongBanID")] NhanVien nhanVien)
        {
            if (ModelState.IsValid)
            {
                db.NhanViens.Add(nhanVien);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.CapBacID = new SelectList(db.CapBacs, "ID", "TenCapBac", nhanVien.CapBacID);
            ViewBag.ChucDanhID = new SelectList(db.ChucDanhs, "ID", "TenChucDanh", nhanVien.ChucDanhID);
            ViewBag.ChucVuID = new SelectList(db.ChucVus, "ID", "TenChucVu", nhanVien.ChucVuID);
            ViewBag.HocViID = new SelectList(db.HocVis, "ID", "TenHocVi", nhanVien.HocViID);
            ViewBag.PhongBanID = new SelectList(db.PhongBans, "ID", "TenPhongBan", nhanVien.PhongBanID);
            return View(nhanVien);
        }

        // GET: Admin/NhanViens/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            NhanVien nhanVien = await db.NhanViens.FindAsync(id);
            if (nhanVien == null)
            {
                return HttpNotFound();
            }
            ViewBag.CapBacID = new SelectList(db.CapBacs, "ID", "TenCapBac", nhanVien.CapBacID);
            ViewBag.ChucDanhID = new SelectList(db.ChucDanhs, "ID", "TenChucDanh", nhanVien.ChucDanhID);
            ViewBag.ChucVuID = new SelectList(db.ChucVus, "ID", "TenChucVu", nhanVien.ChucVuID);
            ViewBag.HocViID = new SelectList(db.HocVis, "ID", "TenHocVi", nhanVien.HocViID);
            ViewBag.PhongBanID = new SelectList(db.PhongBans, "ID", "TenPhongBan", nhanVien.PhongBanID);
            return View(nhanVien);
        }

        // POST: Admin/NhanViens/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "ID,HoTen,ChucVuID,CapBacID,HocViID,ChucDanhID,PhongBanID")] NhanVien nhanVien)
        {
            if (ModelState.IsValid)
            {
                db.Entry(nhanVien).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.CapBacID = new SelectList(db.CapBacs, "ID", "TenCapBac", nhanVien.CapBacID);
            ViewBag.ChucDanhID = new SelectList(db.ChucDanhs, "ID", "TenChucDanh", nhanVien.ChucDanhID);
            ViewBag.ChucVuID = new SelectList(db.ChucVus, "ID", "TenChucVu", nhanVien.ChucVuID);
            ViewBag.HocViID = new SelectList(db.HocVis, "ID", "TenHocVi", nhanVien.HocViID);
            ViewBag.PhongBanID = new SelectList(db.PhongBans, "ID", "TenPhongBan", nhanVien.PhongBanID);
            return View(nhanVien);
        }

        // GET: Admin/NhanViens/Delete/5
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            NhanVien nhanVien = await db.NhanViens.FindAsync(id);
            if (nhanVien == null)
            {
                return HttpNotFound();
            }
            return View(nhanVien);
        }

        // POST: Admin/NhanViens/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            NhanVien nhanVien = await db.NhanViens.FindAsync(id);
            db.NhanViens.Remove(nhanVien);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
